package cn.sinata.wanglin.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.NewFriend;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.ImageUtil;

/**
 *
 * Created by liaoxiang on 16/12/10.
 */

public class NewFriendsAdapter extends HFRecyclerAdapter<NewFriend> {
    private String userId;
    public NewFriendsAdapter(List<NewFriend> mData) {
        super(mData, R.layout.item_new_friend_layout);
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
    }

    @Override
    public void onBind(int position, final NewFriend newFriend, ViewHolder holder) {
        SimpleDraweeView headView = holder.bind(R.id.headView);
        headView.setImageURI(ImageUtil.parseImageUri(newFriend.getHeadUrl()));
        holder.setText(R.id.tv_name,newFriend.getNickName());
        final int status = newFriend.getStatus();
        final String applyType = newFriend.getApplyType();
        String content = "";
        //如果是群信息。
        if (applyType.equals("G")) {
            content = TextUtils.isEmpty(newFriend.getContent())?"申请加入"+newFriend.getGroupName()+"群"
                    :"申请加入"+newFriend.getGroupName()+"群:"+newFriend.getContent();
        } else {
            content = newFriend.getContent();
        }
        holder.setText(R.id.tv_content,content);
        TextView tv_action = holder.bind(R.id.tv_action);
        tv_action.setText(newFriend.getActionStr());
        //验证状态（（0：接受 1：验证通过 3：等待验证 4：拒绝））

        tv_action.setSelected(status==0);

        tv_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status == 0 && applyType.equals("F")) {
                    acceptFriendAuth(newFriend);
                } else if (status == 0 && applyType.equals("G")) {
                    processGroupApply(newFriend);
                }
            }
        });
    }

    private void acceptFriendAuth(final NewFriend newFriend){

        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showDialog();
        }
        //状态（1：申请通过 2，申请不通过，3删除）
        HttpManager.disposeApplyFriend(userId,1,newFriend.getId())
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        //已接受。
                        newFriend.setStatus(1);
                        notifyDataSetChanged();
                    }
                });
    }

    private void processGroupApply(final NewFriend newFriend){

        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showDialog();
        }
        //状态（1：申请通过 2，申请不通过，3删除）
        HttpManager.processApply(newFriend.getId())
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        //已接受。
                        newFriend.setStatus(1);
                        notifyDataSetChanged();
                    }
                });
    }
}

